package KC.cimaV2;

import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.ArrayList;
import java.sql.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class liquidacionPeso {

    public static String id_Lote = null;
    public static String id_Almac = null;
    public static String nombre_Almac = null;
    public static Object data2 = null;

    public static int countRow;

    public static String retorno = "";

    public liquidacionPeso() {
    }

    public static Object[] obtenerAlmacen(Statement stRs, String id_contrato) throws
            SQLException {

        String descripcion_c ="";
        String idmotivo_Lote ="";
        String str_motivo = "";
        ArrayList array_Ret = new ArrayList();

        String query =
            " SELECT  contrato.descripcion, "+
            "         lote, " +
            "         codigo_baan + '-' + convert( varchar, anio ) as motivo " +
            " FROM    contrato " +
            "                left join lote " +
            "                on id_lote = lote " +
            " WHERE   id_contrato = " + id_contrato;

        ResultSet queryRs;

        queryRs = stRs.executeQuery(query);

       while(queryRs.next()){
           idmotivo_Lote = queryRs.getString("lote");
           descripcion_c = queryRs.getString("descripcion");
           str_motivo = queryRs.getString("motivo");

           Object[] registros = {
                                      idmotivo_Lote,
                                      descripcion_c,
                                      str_motivo
            };
            array_Ret.add(registros);
       }
       stRs.close();
        return array_Ret.toArray();
    }

    public static Object[] ramaContrato(Statement stmt, String idmotivo_lote) throws
            SQLException {
        ArrayList array_Ret = new ArrayList();
        String id_almacen;
        String nombre_almacen;
        ResultSet queryRs;

        String query2 = "SELECT DISTINCT a.id_almacen," +
                        "a.nombre "+
                        "FROM VISTA_UNIDAD vu," +
                        "ALMACEN a "+
                        "WHERE a.id_almacen=vu.almacen_destino " +
                        " AND vu.id_lote =" + idmotivo_lote +
                        " AND vu.status = 'Entrada por contrato' "+
                        "ORDER BY a.nombre";


         queryRs = stmt.executeQuery(query2);

         while(queryRs.next()){
             id_almacen = queryRs.getString("id_almacen");
             nombre_almacen = queryRs.getString("nombre");

             Object[] registros = {
                                  id_almacen,
                                  nombre_almacen
             };
             array_Ret.add(registros);
         }
         stmt.close();
         return array_Ret.toArray();
     }

     public static String rama(Connection ConnRs2, String idalmacen, String idmotivolote,String idorigen, int secuencia)
             throws SQLException{
        String retornar="";

            String query2;
            ResultSet queryRs2;
            PreparedStatement StatementRs2=null;

            retornar+="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
            query2="SELECT DISTINCT a.id_almacen,"+
                   "a.nombre" +
                   " FROM VISTA_UNIDAD vu, "+
                   "ALMACEN a WHERE a.id_almacen=vu.almacen_destino" +
                   " AND vu.id_lote = "+ idmotivolote +
                   " AND vu.almacen_origen = "+ idalmacen +
                   " AND  (vu.status = 'Transito' OR vu.status = 'Recepcionado')" +
                   " ORDER BY a.nombre";

            //out.println(query2);
            StatementRs2 = ConnRs2.prepareStatement(query2);
            queryRs2 = StatementRs2.executeQuery();

            int i =0;

            while(queryRs2.next()){
                if(i > 0)
                    countRow++;
                i++;
                String id_almacen = queryRs2.getString("id_almacen");
                retornar+="  <tr>"; //            liquidacion_maiz2.jsp?id_almac="+id_almacen+"&IdMotivoLote="+idmotivolote+"&banderaML=0
                retornar+="  <td class=\"textcolumna\" width=\"10\" align=\"center\" valign=\"top\"><font size=\"2\">&raquo;</font></td>";
                retornar+="  <td class=\"textcolumna\" valign=\"top\"><a href=\"javascript:abrirliquidacion2("+id_almacen+","+idmotivolote+")\">"+(((data2=queryRs2.getObject("nombre"))==null)?"":data2)+"</a></td>";
                if(!idorigen.equals(id_almacen)){
                    retornar+="  <td class=\"textcolumna\" valign=\"top\">"+rama(ConnRs2, id_almacen, idmotivolote, idorigen, secuencia)+"</td>";

                }else{
                    retornar+="  <td class=\"textcolumna\" valign=\"top\">&nbsp;</td>";
                }
                retornar+="  </tr>";
            }queryRs2.close();
            retornar+="</table>";

            return retornar;
    } // Eo String rama
}

